﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Browser;


namespace AnythingTheyCanDo.LinqToDom
{
    /// <summary>
    /// abstract basis for all dom nodes
    /// </summary>
    public class DomNode : INotifyPropertyChanged
    {


        #region Property Backing Stores
        private HtmlElement backingDomNode;
        private string id;
        private string cssClass;
        private List<string> cssClasses = new List<string>();
        private string style;
        private bool hasId;
        #endregion

        #region Public Properties
        public string Style
        {
            get { return style; }
            set
            {
                if (value != style)
                {
                    style = value;
                }
            }
        }

        public List<string> CssClasses
        {
            get { return cssClasses; }
        }

        public string CssClass
        {
            get { return cssClass; }
            set
            {
                if (cssClass != value)
                {

                    cssClass = value;
                    setClassesFromString();
                    sendChange("ClassString");
                }
            }
        }

        public string Id
        {
            get { return id; }
            set
            {
                if (id != value)
                {
                    id = value;
                }
            }
        }

        public HtmlElement BackingDomNode
        {
            get { return backingDomNode; }
        }

        public bool HasId
        {
            get { return hasId; }
        }

        public string NodeType
        {
            get { return getAttributeOrStringNull("nodeName"); }
        }
        #endregion

        internal DomNode(HtmlElement el)
        {
            this.backingDomNode = el;
            this.CssClass = el.CssClass;
            this.Id = getAttributeOrStringNull("id");
            this.hasId = Id != "";
            this.Style = getAttributeOrStringNull("style");

        }
        private void setClassesFromString()
        {
            cssClasses.Clear();
            this.cssClass.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(
                    s => CssClasses.Add(s));
        }

        private string getClassString()
        {
            string str = "";
            CssClasses.ForEach(s => str += s + " ");
            return str.Substring(0, str.Length - 1);
        }
        protected string getAttributeOrStringNull(string name)
        {
            string attribute = backingDomNode.GetAttribute(name);
            if (attribute == null)
                return "";
            return attribute;
        }

        #region INotifyPropertyChanged Members
        protected void sendChange(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
        public override string ToString()
        {
            return getAttributeOrStringNull("nodeName");
        }
    }


    public class Anchor : DomNode
    {
        private string herf;
        private string target;
        private string title;
        private string innerHTML;

        internal Anchor(HtmlElement el)
            : base(el)
        {

        }

        public string Title
        {
            get { return title; }
            set
            {
                if (title != value)
                {
                    title = value;
                    sendChange("Title");
                }
            }
        }

        public string Target
        {
            get { return target; }
            set
            {
                if (target != value)
                {
                    target = value;
                    sendChange("Target");
                }
            }
        }

        public string Herf
        {
            get { return herf; }
            set
            {
                if (herf != value)
                {
                    herf = value;
                    sendChange("Herf");
                }
            }
        }

        public string InnerHtml
        {
            get { return innerHTML; }
            set
            {
                if (innerHTML != value)
                {
                    innerHTML = value;
                    sendChange("InnerHTML");
                }
            }
        }
    }
    public class Span : DomNode
    {
        private string title;
        private string innerHTML;

        internal Span(HtmlElement el)
            : base(el)
        {

        }

        public string Title
        {
            get { return title; }
            set
            {
                if (title != value)
                {
                    title = value;
                    sendChange("Title");
                }
            }
        }

        public string InnerHtml
        {
            get { return innerHTML; }
            set
            {
                if (innerHTML != value)
                {
                    innerHTML = value;
                    sendChange("InnerHTML");
                }
            }
        }
    }
    public class Div : DomNode
    {
        private string title;
        private string innerHTML;

        internal Div(HtmlElement el)
            : base(el)
        {

        }

        public string Title
        {
            get { return title; }
            set
            {
                if (title != value)
                {
                    title = value;
                    sendChange("Title");
                }
            }
        }

        public string InnerHtml
        {
            get { return innerHTML; }
            set
            {
                if (innerHTML != value)
                {
                    innerHTML = value;
                    sendChange("InnerHTML");
                }
            }
        }
    }
}